package com.dragon.modules.yuegou.service.impl.member;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.web.PageRequest;
import com.dragon.modules.yuegou.entity.member.MemberCardLevelEntity;
import com.dragon.modules.yuegou.entity.member.MemberGradeLogEntity;
import com.dragon.modules.yuegou.entity.member.ProDiscountEntity;
import com.dragon.modules.yuegou.entity.member.ProMagnificationPointEntity;
import com.dragon.modules.yuegou.entity.member.ProRightsEntity;
import com.dragon.modules.yuegou.entity.member.TemplateGradeEntity;
import com.dragon.modules.yuegou.entity.member.TemplateGradeSetEntity;
import com.dragon.modules.yuegou.entity.member.TemplateRightsEntity;
import com.dragon.modules.yuegou.enums.member.MemberCardLevelGradeRule;
import com.dragon.modules.yuegou.enums.member.MemberGradeLogGradeRule;
import com.dragon.modules.yuegou.enums.member.MemberGradeLogType;
import com.dragon.modules.yuegou.enums.member.TemplateGradeGradeRule;
import com.dragon.modules.yuegou.enums.member.TemplateRightsRightsType;
import com.dragon.modules.yuegou.enums.member.TemplateSetGradeRule;
import com.dragon.modules.yuegou.enums.member.TemplateSetStatus;
import com.dragon.modules.yuegou.mapper.member.MemberCardLevelMapper;
import com.dragon.modules.yuegou.model.member.MemberCardLevel;
import com.dragon.modules.yuegou.model.member.MemberCardLevelCache;
import com.dragon.modules.yuegou.model.member.MemberCardLevelModel;
import com.dragon.modules.yuegou.model.member.MemberGradeModel;
import com.dragon.modules.yuegou.model.member.MemberGradeSetModel;
import com.dragon.modules.yuegou.model.member.MemberInfoModel;
import com.dragon.modules.yuegou.model.member.MemberPretendModel;
import com.dragon.modules.yuegou.model.member.ProDiscount;
import com.dragon.modules.yuegou.model.member.ProMagnificationPoint;
import com.dragon.modules.yuegou.model.member.TemplateGrade;
import com.dragon.modules.yuegou.model.member.TemplatePretend;
import com.dragon.modules.yuegou.model.member.TemplateSet;
import com.dragon.modules.yuegou.query.member.MemberCardLevelQuery;
import com.dragon.modules.yuegou.query.member.ProRightsQuery;
import com.dragon.modules.yuegou.query.member.TemplateGradeQuery;
import com.dragon.modules.yuegou.query.member.TemplateGradeSetQuery;
import com.dragon.modules.yuegou.query.member.TemplatePretendQuery;
import com.dragon.modules.yuegou.query.member.TemplateRightsQuery;
import com.dragon.modules.yuegou.response.MemberBenefitResponse;
import com.dragon.modules.yuegou.service.member.BenefitService;
import com.dragon.modules.yuegou.service.member.MemberCardLevelService;
import com.dragon.modules.yuegou.service.member.MemberGradeLogService;
import com.dragon.modules.yuegou.service.member.ProDiscountService;
import com.dragon.modules.yuegou.service.member.ProMagnificationPointService;
import com.dragon.modules.yuegou.service.member.ProRightsService;
import com.dragon.modules.yuegou.service.member.TemplateGradeService;
import com.dragon.modules.yuegou.service.member.TemplateGradeSetService;
import com.dragon.modules.yuegou.service.member.TemplatePretendService;
import com.dragon.modules.yuegou.service.member.TemplateRightsService;
import com.dragon.modules.yuegou.service.member.TemplateSetService;
import com.dragon.modules.yuegou.utils.PageToResultUtils;
import com.dragon.modules.yuegou.web.Check;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * 会员卡等级表 服务实现
 *
 * @author pbuilder generated
 * @date 2021-12-17 16:51:24
 * @version 2.0
 */
@Slf4j
@Service("memMemberCardLevelService")
public class MemberCardLevelServiceImpl
		extends ServiceImpl<MemberCardLevelMapper, MemberCardLevelEntity>
		implements MemberCardLevelService {
	private static final String MEMBER_INFO_MODEL_CACHE = "member_Info_Model_Cache";

	@Autowired
	private TemplateSetService templateSetService;

	@Autowired
	private TemplateRightsService templateRightsService;

	@Autowired
	private TemplateGradeService templateGradeService;

	@Autowired
	private TemplatePretendService templatePretendService;

	@Autowired
	private TemplateGradeSetService templateGradeSetService;

	@Autowired
	private ProDiscountService proDiscountService;

	@Autowired
	private ProMagnificationPointService proMagnificationPointService;

	@Autowired
	private ProRightsService proRightsService;

	private Cache memberInfoModelCache;

	@Autowired
	private BenefitService benefitService;

	@Autowired
	private MemberGradeLogService memberGradeLogService;

	private Random random = new Random();

	private static final String BUSD_CONGIG = "未获取到商圈对应的会员卡设置：";

	@Autowired
	public void setMemberInfoModelCacheManager(CacheManager cacheManager) {
		this.memberInfoModelCache = cacheManager.getCache(MEMBER_INFO_MODEL_CACHE);
	}

	@Override
	public MemberInfoModel getMemberCardlevelInfo(String busdId, String memberId) {
		return getMemberCardlevelInfo(busdId,memberId,null);
	}

	public MemberCardLevel getMemberCardLevel(String busdId, String memberId){
		if (StringUtils.isEmpty(busdId) || StringUtils.isEmpty(memberId)){
			return new MemberCardLevel();
		}
		MemberCardLevelCache memberCardLevelCache = this.getMemberCardLevelCache(busdId,memberId);
		if (memberCardLevelCache==null){
			return new MemberCardLevel();
		}
		return memberCardLevelCache.getMemberCardLevel();
	}

	private MemberCardLevelCache getMemberCardLevelCache(String busdId,String memberId){
		if (StringUtils.isEmpty(busdId) || StringUtils.isEmpty(memberId)){
			return null;
		}

		MemberCardLevelCache memberCardLevelCache = memberInfoModelCache.get(busdId+memberId,MemberCardLevelCache.class);
		if(memberCardLevelCache == null ){
			memberCardLevelCache = new MemberCardLevelCache();
			MemberCardLevelQuery cardLevelQuery = new MemberCardLevelQuery();
			cardLevelQuery.setBusdId(busdId);
			cardLevelQuery.setMemberId(memberId);
			MemberCardLevel cardLevel = getFirst(cardLevelQuery);
			memberCardLevelCache.setMemberCardLevel(cardLevel);

			if(null == cardLevel) {
				log.error("未获取到会员卡等级信息：商圈id={}，会员id={}", busdId, memberId);
				return null;
			}

			// 获取会员卡模板设置
			TemplateSet templateSet = templateSetService.getTemplateSetByBusdId(busdId, TemplateSetStatus.PUBLISH);
			memberCardLevelCache.setTemplateSet(templateSet);
			if(null == templateSet) {
				log.error(BUSD_CONGIG+"商圈id={}，会员id={}", busdId, memberId);
				return null;
			}
			memberInfoModelCache.put(busdId+memberId,memberCardLevelCache);
		}
		return memberCardLevelCache;
	}

	private MemberCardLevel getFirst(MemberCardLevelQuery query){
		QueryWrapper<MemberCardLevelEntity> wrapper = query.toQueryWrapper();
		List<MemberCardLevelEntity> refList = list(wrapper);
		if (CollectionUtils.isNotEmpty(refList)){
			return new MemberCardLevel(refList.get(0));
		}
		return new MemberCardLevel();
	}

	@Override
	public MemberInfoModel getMemberCardlevelInfo(String busdId, String memberId,String goodsId) {
		MemberCardLevelCache memberCardLevelCache = this.getMemberCardLevelCache(busdId,memberId);
		if (memberCardLevelCache==null){
			return new MemberInfoModel();
		}

		MemberInfoModel model = new MemberInfoModel();
		BeanUtils.copyProperties(memberCardLevelCache.getMemberCardLevel(), model);

		MemberBenefitResponse memberBenefit = benefitService.getMemberBenefit(busdId);
		TemplateRightsQuery rightsQuery = new TemplateRightsQuery();
		if(memberBenefit.isDiscountStatus()){
			// 获取会员卡关联等级权益：商品折扣

			rightsQuery.setTemplateSetId(memberCardLevelCache.getTemplateSet().getId());
			rightsQuery.setTemplateGradeId(memberCardLevelCache.getMemberCardLevel().getGradeId());
			rightsQuery.setRightsType(TemplateRightsRightsType.DISCOUNT);
			TemplateRightsEntity discountRights = templateRightsService.getOne(rightsQuery.toQueryWrapper());

			// 商品折扣信息
			if(discountRights != null ) {
				ProDiscountEntity discounts = proDiscountService.getById(discountRights.getMemRightsId());
				model.setProDiscount(new ProDiscount(discounts));
			}
		}

		// 获取会员卡关联等级权益：多倍积分
		if(memberBenefit.isMagnificationPointStatus()){
			rightsQuery = new TemplateRightsQuery();
			rightsQuery.setTemplateSetId(memberCardLevelCache.getTemplateSet().getId());
			rightsQuery.setTemplateGradeId(memberCardLevelCache.getMemberCardLevel().getGradeId());
			rightsQuery.setRightsType(TemplateRightsRightsType.SCORE);
			TemplateRightsEntity scoreRights = templateRightsService.getOne(rightsQuery.toQueryWrapper());

			// 商品倍率积分/多倍积分信息
			if( scoreRights  != null ) {
				ProMagnificationPointEntity point = proMagnificationPointService.getById(scoreRights.getMemRightsId());
				model.setProMagnificationPoint(new ProMagnificationPoint(point));
			}
		}

		if(StringUtils.isEmpty(goodsId) || model.getProDiscount() == null ){
			model.setDiscountStatus(false);
		}else {
			ProRightsQuery query = new ProRightsQuery();
			query.setGoodsId(goodsId);
			query.setRightsId(model.getProDiscount().getId());
			List<ProRightsEntity> proRights = proRightsService.list(query.toQueryWrapper());
			model.setDiscountStatus(CollectionUtils.isNotEmpty(proRights));
		}
		return model;
	}

	@Override
	public void evict(String busdId, String memberId) {
		if (StringUtils.isNotEmpty(busdId) && StringUtils.isNotEmpty(memberId)){
			memberInfoModelCache.evict(busdId+memberId);
		}
	}

	@Override
	public MemberCardLevelModel memberCardList(String busdId, String memberId) {
		Check.notNull(busdId, "busdId is required");
		Check.notNull(memberId, "memberId is required");
		MemberCardLevelModel cardLevelModel = new MemberCardLevelModel();
		// 获取当前客户的会员卡
		MemberCardLevelQuery cardLevelQuery = new MemberCardLevelQuery();
		cardLevelQuery.setMemberId(memberId);
		cardLevelQuery.setBusdId(busdId);
		MemberCardLevel cardLevel = getFirst(cardLevelQuery);
		if(null != cardLevel) {
			BeanUtils.copyProperties(cardLevel, cardLevelModel);
			cardLevelModel.setGradeRule(cardLevel.getGradeRule().getCode());

			// 获取当前商圈下的会员卡等级模板
			TemplateSet templateSet = templateSetService.getTemplateSetByBusdId(cardLevel.getBusdId(), TemplateSetStatus.PUBLISH);
			if(null == templateSet) {
				log.error(BUSD_CONGIG+"商圈id%s", busdId);
				return cardLevelModel;
			}

			cardLevelModel.setCardName(templateSet.getName());

			// 获取当前商圈的会员卡等级装扮
			MemberPretendModel pretendModel = new MemberPretendModel();
			TemplatePretendQuery pretendQuery = new TemplatePretendQuery();
			pretendQuery.setTemplateSetId(templateSet.getId());
			pretendQuery.setTemplateGradeId(cardLevel.getGradeId());
			TemplatePretend pretend = templatePretendService.getFirst(pretendQuery);
			if(null != pretend) {
				BeanUtils.copyProperties(pretend, pretendModel);
			}
			cardLevelModel.setMemberPretend(pretendModel);

			// 会员卡等级列表
			List<MemberGradeModel> templateGradeList = new ArrayList<>();
			TemplateGradeQuery gradeQuery = new TemplateGradeQuery();
			gradeQuery.setStatus(TemplateSetStatus.PUBLISH);
			gradeQuery.setTemplateSetId(templateSet.getId());
			gradeQuery.setDel(false);
			QueryWrapper<TemplateGradeEntity> wrapper = gradeQuery.toQueryWrapper();
			wrapper.orderByAsc(TemplateGradeEntity.COL_SERIAL_NUMBER);
			List<TemplateGradeEntity> grades = templateGradeService.list(wrapper);
			if(CollectionUtils.isNotEmpty(grades)) {
				for(TemplateGradeEntity grade : grades) {
					templateGradeList.add(setMemberGradeModel(grade,cardLevel,cardLevelModel));
				}
			}
			cardLevelModel.setMemberGrades(templateGradeList);
		}
		return cardLevelModel;
	}

	private MemberGradeModel setMemberGradeModel(TemplateGradeEntity grade,MemberCardLevel cardLevel,MemberCardLevelModel cardLevelModel){
		MemberGradeModel gradeModel = new MemberGradeModel();
		BeanUtils.copyProperties(grade, gradeModel);
		gradeModel.setGradeRule(grade.getGradeRule().getCode());
		gradeModel.setSelected(false);
		if(grade.getId().equals(cardLevel.getGradeId())) {
			gradeModel.setSelected(true);
			cardLevelModel.setLevelGrow(grade.getScoreEnd());
			cardLevelModel.setSerialNumber(grade.getSerialNumber());
		}
		// 获取该等级的会员装扮
		MemberPretendModel gradePretendModel = new MemberPretendModel();
		TemplatePretendQuery gradePretendQuery = new TemplatePretendQuery();
		gradePretendQuery.setTemplateSetId(grade.getTemplateSetId());
		gradePretendQuery.setTemplateGradeId(grade.getId());
		TemplatePretend gradePretend = templatePretendService.getFirst(gradePretendQuery);
		if(null != gradePretend) {
			BeanUtils.copyProperties(gradePretend, gradePretendModel);
		}
		gradeModel.setGradePretend(gradePretendModel);
		return gradeModel;
	}

	@Override
	public List<MemberGradeModel> getMemberGrades(String busdId) {
		// 获取当前商圈下的会员卡等级模板
		TemplateSet templateSet = templateSetService.getTemplateSetByBusdId(busdId, TemplateSetStatus.PUBLISH);
		if(null == templateSet) {
			log.error(BUSD_CONGIG+"商圈id={}", busdId);
			return null;
		}

		// 会员卡等级列表
		List<MemberGradeModel> templateGradeList = new ArrayList<>();
		TemplateGradeQuery gradeQuery = new TemplateGradeQuery();
		gradeQuery.setTemplateSetId(templateSet.getId());
		gradeQuery.setDel(false);
		QueryWrapper<TemplateGradeEntity> wrapper = gradeQuery.toQueryWrapper();
		wrapper.orderByAsc(TemplateGradeEntity.COL_SERIAL_NUMBER);
		List<TemplateGradeEntity> grades = templateGradeService.list(wrapper);
		if(CollectionUtils.isNotEmpty(grades)) {
			for(TemplateGradeEntity grade : grades) {
				MemberGradeModel gradeModel = new MemberGradeModel();
				BeanUtils.copyProperties(grade, gradeModel);
				gradeModel.setGradeRule(grade.getGradeRule().getCode());
				gradeModel.setSelected(false);

				// 当前商圈的会员卡等级装扮
				MemberPretendModel pretendModel = new MemberPretendModel();
				TemplatePretendQuery pretendQuery = new TemplatePretendQuery();
				pretendQuery.setTemplateSetId(grade.getTemplateSetId());
				pretendQuery.setTemplateGradeId(grade.getId());
				TemplatePretend pretend = templatePretendService.getFirst(pretendQuery);
				if(null != pretend) {
					BeanUtils.copyProperties(pretend, pretendModel);
				}
				gradeModel.setGradePretend(pretendModel);
				templateGradeList.add(gradeModel);
			}
		}
		return templateGradeList;
	}

	@Override
	public List<MemberGradeSetModel> getMemberGradeSets(String busdId) {
		Check.notNull(busdId, "busdId is required");
		List<MemberGradeSetModel> models = new ArrayList<>();
		// 获取当前商圈下的会员卡等级模板
		TemplateSet templateSet = templateSetService.getTemplateSetByBusdId(busdId, TemplateSetStatus.PUBLISH);
		if(null == templateSet) {
			log.error(BUSD_CONGIG+"商圈id={}", busdId);
			return null;
		}

		// 会员卡等级列表
		TemplateGradeQuery gradeQuery = new TemplateGradeQuery();
		gradeQuery.setTemplateSetId(templateSet.getId());
		gradeQuery.setDel(false);
		QueryWrapper<TemplateGradeEntity> wrapper = gradeQuery.toQueryWrapper();
		wrapper.orderByAsc(TemplateGradeEntity.COL_SERIAL_NUMBER);
		List<TemplateGradeEntity> grades = templateGradeService.list(wrapper);
		if(CollectionUtils.isNotEmpty(grades)) {
			for(TemplateGradeEntity grade : grades) {
				models.add(this.setMemberGradeSetModel(grade));
			}
		}
		return models;
	}

	private MemberGradeSetModel setMemberGradeSetModel(TemplateGradeEntity grade){
		MemberGradeSetModel model = new MemberGradeSetModel();
		BeanUtils.copyProperties(grade, model);
		model.setGradeRule(grade.getGradeRule().getCode());
		if(TemplateSetGradeRule.SPECIFIED.getCode().equals(grade.getGradeRule().getCode())) {
			// 获取会员卡当前等级已选等级条件
			TemplateGradeSetQuery gradeSetQuery = new TemplateGradeSetQuery();
			gradeSetQuery.setTemplateSetId(grade.getTemplateSetId());
			gradeSetQuery.setTemplateGradeId(grade.getId());
			gradeSetQuery.memGradeParamId().isNotNull();
			List<TemplateGradeSetEntity> gradeSets = templateGradeSetService.list(gradeSetQuery.toQueryWrapper());
			if(CollectionUtils.isNotEmpty(gradeSets)) {
				List<String> gradeParams = gradeSets.stream().map(TemplateGradeSetEntity::getMemGradeParamName).collect(Collectors.toList());
				model.setGradeParams(gradeParams);
				model.setGradeCondition(gradeSets.get(0).getGradeCondition().getCode());
			}
		} else {
			StringBuilder sb = new StringBuilder();
			if(null == grade.getScoreEnd()) {
				sb.append("成长值累计大于").append(grade.getScoreStart()).append("分即可；");
			} else {
				sb.append("成长值累计").append(grade.getScoreStart()).append("-").append(grade.getScoreEnd()).append("分即可；");
			}
			model.setDesc(sb.toString());
		}
		return model;
	}


	/**
	 * 获取会员的会员卡等级信息
	 * @param busdId
	 * @param memberId
	 */
	@Override
	public MemberCardLevel initMemberCardLevel(String busdId, String memberId) {
		try {
			if(StringUtils.isEmpty(busdId) || StringUtils.isEmpty(memberId)) {
				log.error("商圈ID和会员ID条件不能为空：商圈id={}，会员id={}", busdId, memberId);
				return new MemberCardLevel();
			}

			createMemberCardLevel(busdId, memberId);
			// 未获取到会员卡等级信息，创建会员的会员卡等级信息
		} catch (Exception e) {
			log.error("获取会员的会员卡等级信息失败，{}", e.getMessage());
		}
		return new MemberCardLevel();
	}

	@Override
	public List<MemberCardLevel> memberCardLevelList(PageRequest pageInfo, String busdId, String gradeId){
		MemberCardLevelQuery levelQuery = new MemberCardLevelQuery();
		levelQuery.busdId().eq(busdId);
		if (StringUtils.isNotEmpty(gradeId)){
			levelQuery.gradeId().eq(gradeId);
		}
		IPage<MemberCardLevel> page = this.page(PageToResultUtils.resultToPage(pageInfo),levelQuery.toQueryWrapper());
		return page.getRecords();
	}

	/**
	 * 创建会员的会员卡等级信息
	 * @param busdId
	 * @param memberId
	 */
	private MemberCardLevel createMemberCardLevel(String busdId, String memberId) {
		try {
			if(StringUtils.isEmpty(busdId) || StringUtils.isEmpty(memberId)) {
				log.error("商圈ID和会员ID条件不能为空：商圈id={}，会员id={}", busdId, memberId);
				return new MemberCardLevel();
			}

			// 判断当前会员是否已有会员卡等级
			MemberCardLevelQuery cardLevelQuery = new MemberCardLevelQuery();
			cardLevelQuery.setBusdId(busdId);
			cardLevelQuery.setMemberId(memberId);
			MemberCardLevel memberCardLevel = this.getFirst(cardLevelQuery);
			if(null != memberCardLevel) {
				log.debug("根据商圈id与会员id获取到会员卡等级信息，无需创建：商圈id={}，会员id={}", busdId, memberId);
				return new MemberCardLevel();
			}

			// 获取当前商圈下的会员卡等级模板
			TemplateSet templateSet = templateSetService.getTemplateSetByBusdId(busdId, TemplateSetStatus.PUBLISH);
			if(null == templateSet) {
				log.error("当前商圈未配置会员卡等级设置，busdId={}", busdId);
				return new MemberCardLevel();
			}

			// 获取会员卡初始等级
			TemplateGradeQuery gradeQuery = new TemplateGradeQuery();
			gradeQuery.setTemplateSetId(templateSet.getId());
			gradeQuery.setDel(false);
			gradeQuery.setOrigin(true);
			gradeQuery.setGradeRule(TemplateGradeGradeRule.from(templateSet.getGradeRule().getCode()));
			TemplateGrade templateGrade = templateGradeService.getFirst(gradeQuery);
			if(null == templateGrade) {
				log.error("当前商圈未配置会员卡等级条件，busdId={}", busdId);
				return new MemberCardLevel();
			}

			MemberCardLevel memberCardLevelEntity = new MemberCardLevel();
			memberCardLevelEntity.setId(IdWorker.getIdStr());
			memberCardLevelEntity.setBusdId(busdId);
			memberCardLevelEntity.setMemberId(memberId);
			memberCardLevelEntity.setBeginTime(LocalDateTime.now());
			// 会员卡号：前缀+22位后缀，后缀自动生成
			memberCardLevelEntity.setCardNo(templateSet.getBizNoPrefix() + initCardNo());
			memberCardLevelEntity.setGradeRule(MemberCardLevelGradeRule.from(templateSet.getGradeRule().getCode()));
			memberCardLevelEntity.setGradeId(templateGrade.getId());
			memberCardLevelEntity.setGradeName(templateGrade.getName());
			if(TemplateSetGradeRule.GROWTH.getCode().equals(templateSet.getGradeRule().getCode())) {
				memberCardLevelEntity.setGrowCount(0L);
			}
			memberCardLevelEntity.setCreatedTime(LocalDateTime.now());
			this.baseMapper.insert(memberCardLevelEntity);

			// 创建会员等级更新日志
			MemberGradeLogEntity gradeLog = new MemberGradeLogEntity();
			gradeLog.setId(IdWorker.getIdStr());
			gradeLog.setBusdId(busdId);
			gradeLog.setMemberId(memberId);
			gradeLog.setType(MemberGradeLogType.VIP_CREATE);
			gradeLog.setGradeRule(MemberGradeLogGradeRule.from(templateSet.getGradeRule().getCode()));
			gradeLog.setAfterGrade(templateGrade.getName());
			gradeLog.setReason("登录注册");
			gradeLog.setCreatedTime(LocalDateTime.now());
			memberGradeLogService.save(gradeLog);

			// 会员进行升降级处理
			templateGradeService.gradeRelegation(busdId, memberId);

			return memberCardLevelEntity;
		} catch (Exception e) {
			log.error("创建会员的会员卡等级信息失败，{}", e.getMessage());
		}
		return new MemberCardLevel();
	}

	/**
	 * 业务卡号后缀：22位，后台自动生成，生成规则：年月日+随机14位数
	 * @return
	 */
	private String initCardNo() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		StringBuilder cardNo = new StringBuilder();
		cardNo.append(sdf.format(date));
		for(int i = 0; i < 14; i++) {
			int a = this.random.nextInt(10);
			cardNo.append(a);
		}

		return cardNo.toString();
	}

}